package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.dto.pojo.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌controller
 *
 * @author xxx
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 分页查询
     *
     * @param key    搜索条件
     * @param page   当前页
     * @param rows   页面大小
     * @param sortBy 排序的字段
     * @param desc   是否排序
     * @return PageResult<BrandDTO>
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<BrandDTO>> brandPageQuery(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer rows,
            @RequestParam(value = "sortBy", defaultValue = "id") String sortBy,
            @RequestParam(value = "desc", defaultValue = "false") Boolean desc) {
        // 返回数据
        return ResponseEntity.ok(brandService.brandPageQuery(key, page, rows, sortBy, desc));
    }

    /**
     * 保存品牌
     * @param brand
     * @param cids
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brand, @RequestParam("cids") List<Long> cids) {
        brandService.saveBrand(brand, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据分类id查询品牌
     * @param categoryId
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(
            @RequestParam("id") Long categoryId){
        return ResponseEntity.ok(this.brandService.queryBrandByCategoryId(categoryId));
    }

    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryById(@PathVariable("id") Long id){
        return ResponseEntity.ok(brandService.queryById(id));
    }

    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(brandService.queryBrandByIds(ids));
    }
}
